package cn.legend.travel.guide.service.impl;

import cn.legend.travel.common.pojo.vo.PageData;
import cn.legend.travel.common.util.PageInfoToPageDataConverter;
import cn.legend.travel.guide.dao.cache.IGuideCacheRepository;
import cn.legend.travel.guide.dao.cache.IGuideDetailCacheRepository;
import cn.legend.travel.guide.dao.persist.repository.IGuideDetailRepository;
import cn.legend.travel.guide.dao.persist.repository.IGuideRepository;
import cn.legend.travel.guide.pojo.vo.GuideDetailCommentVO;
import cn.legend.travel.guide.pojo.vo.GuideDetailVO;
import cn.legend.travel.guide.pojo.vo.GuideVO;
import cn.legend.travel.guide.service.IGuideService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class GuideServiceImpl implements IGuideService {
    @Autowired
    IGuideRepository guideRepository;
    @Autowired
    IGuideCacheRepository guideCacheRepository;
    @Autowired
    IGuideDetailRepository guideDetailRepository;
    @Autowired
    IGuideDetailCacheRepository guideDetailCacheRepository;

    @Override
    public List<Map<String, Object>> getGuideList(Integer pageNum) {

        List<Map<String, Object>> guideList = guideCacheRepository.getGuideList(pageNum);
        if(guideList.isEmpty()){
            List<Map<String, Object>> guideList1 = guideRepository.getGuideList(pageNum);
            guideCacheRepository.setGuideListCache(pageNum,guideList1);
            return guideList1;
        }
        return guideList;
    }

    @Override
    public GuideDetailVO getGuideDetailById(Long guideId) {
        GuideDetailVO guideDetailVO = guideDetailCacheRepository.getGuideDetailById(guideId);
        if(guideDetailVO != null){
            return guideDetailVO;
        }
        GuideDetailVO guideDetailVO1 =  guideDetailRepository.getGuideDetailById(guideId);
        guideDetailCacheRepository.saveGuideDetailById(guideId,guideDetailVO1);
        return guideDetailVO1;
    }

    @Override
    public PageData<GuideDetailCommentVO> getGuideDetailCommentByGuideId(Integer pageNum, Long guideId) {
        log.info("开始执行【攻略详情评论】，页码{}，攻略ID{}",pageNum,guideId);
        PageHelper.startPage(pageNum,3);
        List<GuideDetailCommentVO> comments = guideDetailRepository.getGuideDetailCommentByGuideId(guideId);
        PageInfo<GuideDetailCommentVO> pageInfo = new PageInfo<>(comments);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }
}
